package com.example.book.service;

import com.example.book.enums.BookStatusEnum;
import com.example.book.mapper.BookInfoMapper;
import com.example.book.model.BookInfo;
import com.example.book.model.PageRequest;
import com.example.book.model.PageResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;


//Component:会帮我们new一个BookService的对象，将这些存到spring里面
@Component
public class BookService {

    @Autowired
    private BookInfoMapper bookInfoMapper;



    public Integer addBook(BookInfo bookInfo) {
        return bookInfoMapper.insertBook(bookInfo);
    }


    public  PageResponse<BookInfo> getListByPage(PageRequest pageRequest) {
        //总记录
        Integer count =bookInfoMapper.count();
        //当前页的记录
        List<BookInfo> bookInfos=bookInfoMapper.queryBookByPage(pageRequest);
        for (BookInfo bookInfo:bookInfos){
            bookInfo.setStatusCN(BookStatusEnum.getStatusByCode(bookInfo.getStatus()).getDesc());
        }
//         PageResponse<BookInfo> pageResponce=new PageResponse<>();
//        //当创建PageResponse对象时，它的初始状态里total和records都是null或者空列表
//        //这里的count是一个整数
//         pageResponce.setTotal(count);
//         pageResponce.setRecords(bookInfos);
         return  new PageResponse<BookInfo>(count,bookInfos,pageRequest);
    }

    public BookInfo queryBookById(Integer bookId) {
        return bookInfoMapper.queryBookById(bookId);
    }

    public Integer updateBook(BookInfo bookInfo) {
        return bookInfoMapper.updateBook(bookInfo);
    }

    public void batchDeleteBook(List<Integer> ids) {
        bookInfoMapper.batchDeleteBook(ids);
    }
}
